package com.java.TCP.receive_feedback;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8888);
        
        Scanner sendScanner = new Scanner(System.in);
        Scanner receiveScanner = new Scanner(socket.getInputStream());
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
        
        while (true) {
            // 客户端发送数据
            System.out.println("客户端" + socket.getLocalPort() + "对服务器说 ");
            String send = sendScanner.nextLine();
            printWriter.println(send);
            printWriter.flush();
            
            if ("再见".equals(send)) {
                break;
            }
            
            // 客户端接收反馈
            String receive = receiveScanner.nextLine();
            System.out.println("服务器对客户端" + socket.getLocalPort() + "说 " + receive);
            
            if ("再见".equals(receive)) {
                break;
            }
        }
        
        printWriter.close();
        socket.close();
    }
}
